package com.atguigu.gulimall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsConfigurationSource;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

@Configuration
public class GulimallCorsConfiguration {
    @Bean
    public CorsWebFilter corsWebFilter(){
        //创建CorsWebFilter对象需要传入CorsConfigurationSource配置源
        //而CorsConfigurationSource是一个接口，实现类是reactive包下的UrlBasedCorsConfigurationSource
        // org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource 响应式编程
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        //注册跨域配置 参1设置需要跨域的请求 参2传入跨域配置类
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*");//允许哪些请求头跨域
        corsConfiguration.addAllowedMethod("*");//允许哪些请求方式跨域
        corsConfiguration.addAllowedOrigin("*");//允许哪些请求来源跨域
        corsConfiguration.setAllowCredentials(true);//允许携带cookie跨域
        source.registerCorsConfiguration("/**",corsConfiguration);
        return new CorsWebFilter(source);
    }
}
